//
//  HXReadLeaveMsgTableViewCell.m
//  HXTClient
//
//  Created by lemon on 14-4-22.
//  Copyright (c) 2014年 easaa. All rights reserved.
//

#import "HXReadLeaveMsgTableViewCell.h"
#import "UIImage+HXImage.h"
#import "NSString+HXString.h"
#import "CopyLabel.h"

NSString *const textTypeKey = @"textTypeKey";
NSString *const textContentKey = @"textContentKey";
NSString *const textSenderNameKey = @"textSenderNameKey";
NSString *const textSenderTimeKey = @"textSenderTimeKey";

float const msgMaxWidth = 240.f;
float const msgMinHeight = 25.f;
float const msgFontSize = 14.f;

@interface HXReadLeaveMsgTableViewCell ()
{
    UIImageView *_bgImageView;
    CopyLabel *_contentLabel;
    UILabel *_senderLabel;
}

@end

@implementation HXReadLeaveMsgTableViewCell

- (void)dealloc
{
    HX_RELEASE(_senderLabel)
    HX_RELEASE(_bgImageView)
    HX_RELEASE(_contentLabel)
    HX_RELEASE(_dicContent)
    HX_SUPER_DEALLOC
}

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
        [self initSubView];
    }
    return self;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (void)initSubView
{
    _senderLabel = [[UILabel alloc] initWithFrame:CGRectMake(5.f, 5.f, 200.f, 20.f)];
    _senderLabel.font = [UIFont systemFontOfSize:15.f];
    _senderLabel.textColor = [UIColor blackColor];
    _senderLabel.backgroundColor = [UIColor clearColor];
    [self.contentView addSubview:_senderLabel];
    
    _bgImageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, _senderLabel.bottom, 280, 40)];
    _bgImageView.image = [self leftImage];
    _bgImageView.backgroundColor = [UIColor clearColor];
    _bgImageView.userInteractionEnabled = YES;
    [self.contentView addSubview:_bgImageView];
    
    _contentLabel = [[CopyLabel alloc] initWithFrame:CGRectMake(30, 5.f, msgMaxWidth, msgMinHeight)];
    _contentLabel.backgroundColor = [UIColor clearColor];
    _contentLabel.text = @"中国共产党";
    _contentLabel.textColor = [UIColor blackColor];
    _contentLabel.lineBreakMode = NSLineBreakByWordWrapping;
    _contentLabel.numberOfLines = 0;
    _contentLabel.font = [UIFont boldSystemFontOfSize:msgFontSize];
    [_bgImageView addSubview:_contentLabel];
}

- (UIImage *)leftImage
{
    UIImage *image = [[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"common_leaveMsg" ofType:@"png"]];
    image = [image resizeImageFromInsets:UIEdgeInsetsMake(20, 15, 20, 10)];
    //_bgImageView.image = image;
    return image;
}

- (UIImage *)rightImage
{
    UIImage *image = [UIImage imageNamed:@"common_reply.png"];
    image = [image resizeImageFromInsets:UIEdgeInsetsMake(20, 10, 20, 15)];
    //_bgImageView.image = image;
    return image;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    NSNumber *numType = [self.dicContent objectForKey:textTypeKey];
    NSString *strMsg = [self.dicContent objectForKey:textContentKey];
    CGSize size = [strMsg sizeWithFont:[UIFont boldSystemFontOfSize:msgFontSize]
                      andConstrainedToSize:CGSizeMake(msgMaxWidth, 1000)];
    CGFloat height = 40 + size.height - msgMinHeight;
    if ([numType intValue] == 1)
    {//左
        _senderLabel.left = 5.f;
        _senderLabel.textAlignment = NSTextAlignmentLeft;
        _senderLabel.text = [NSString stringWithFormat:@"%@ %@",
                             [self.dicContent objectForKey:textSenderNameKey],
                             [self.dicContent objectForKey:textSenderTimeKey]];
        
        _bgImageView.image = [self leftImage];
        _bgImageView.frame = CGRectMake(10, _senderLabel.bottom, size.width + 40.f, height);
        _contentLabel.left = 30.f;
    } else if ([numType intValue] == 2)
    {//右
        _senderLabel.left = (ScreenWidth - _senderLabel.width - 5.f);
        _senderLabel.textAlignment = NSTextAlignmentRight;
        _senderLabel.text = [NSString stringWithFormat:@"%@ %@",
                             [self.dicContent objectForKey:textSenderTimeKey],
                             [self.dicContent objectForKey:textSenderNameKey]];
        
        _bgImageView.image = [self rightImage];
        _bgImageView.frame = CGRectMake(ScreenWidth - 10 - (size.width + 40.f), _senderLabel.bottom, size.width + 40.f, height);
        _contentLabel.left = 10.f;
    }
    _contentLabel.height = size.height;
    _contentLabel.text = strMsg;
}

+ (CGFloat)readLeaveMsgTableViewCellHeightWith:(NSDictionary *)dicContent
{
    CGFloat baseHeight = 40.f + 10.f + 25.f;
    NSString *strContent = [dicContent objectForKey:textContentKey];
    CGSize size = [strContent sizeWithFont:[UIFont boldSystemFontOfSize:msgFontSize]
                      andConstrainedToSize:CGSizeMake(msgMaxWidth, 1000)];
    baseHeight += (size.height - msgMinHeight);
    return baseHeight;
}

@end
